package cz.cvut.manatee.client.concordance;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;

import cz.cvut.manatee.client.RadioButtonExtended;
import cz.cvut.manatee.client.Resources;
import cz.cvut.manatee.client.TrimTextBox;
import cz.cvut.manatee.shared.AttributItem;
import cz.cvut.manatee.shared.concordance.CollectedParameters;
import cz.cvut.manatee.shared.concordance.CollectedSimpleOrCqlParameters;

/**
 * Poskytuje vyber mezi Simple dotazem a CQL
 */
public class SimpleOrCqlWidget extends ConcordanceFormWidget {
	private final String KEYWORD_SIMPLE = "Simple";
	private final String KEYWORD_CQL = "CQL";
	private final String KEYWORD_PHRAZE = "fráze";
	private final String KEYWORD_LEMMA = "lemma";

	private RadioButton simpleRadio;
	private RadioButton cqlRadio;
	private TrimTextBox simple;
	private TrimTextBox cql;
	private RadioButton phraze;
	private RadioButton lemma;
	
	private boolean isSimple;
	private boolean isLemmatized;
	
	private VerticalPanel radioPhrazeOrLemma;
	
	HorizontalPanel main;
	
	public SimpleOrCqlWidget() {
		super();
		
		simpleRadio = new RadioButtonExtended("simleorcql", KEYWORD_SIMPLE, true, true);
		cqlRadio    = new RadioButtonExtended("simleorcql", KEYWORD_CQL, true, false);
		simpleRadio.setValue(true);
		
		simple = new TrimTextBox();
		cql    = new TrimTextBox();
		
		simple.setWidth("30ex");
		cql.setWidth("50ex");
		
		main = new HorizontalPanel();
		main.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		VerticalPanel radioSimpleCQL = new VerticalPanel();
		radioSimpleCQL.add(simpleRadio);
		radioSimpleCQL.add(cqlRadio);
		
		phraze = new RadioButtonExtended("phrazeOrLemma", KEYWORD_PHRAZE, true, true);
		lemma  = new RadioButtonExtended("phrazeOrLemma", KEYWORD_LEMMA, true, false);
		phraze.setValue(true);
		
		radioPhrazeOrLemma = new VerticalPanel();
		radioPhrazeOrLemma.add(phraze);
		radioPhrazeOrLemma.add(lemma);
		
		
		main.add(radioSimpleCQL);
		
		this.initWidget(main);
		
		// zobrazeni nebo skryti Simple a CQL formulare na zaklade radiobuttonu
		ValueChangeHandler<Boolean> simpleOrCQLHandler = new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				if (SimpleOrCqlWidget.this.simpleRadio.getValue()) {
					SimpleOrCqlWidget.this.setSimple();
				} else {
					SimpleOrCqlWidget.this.setCQL();
				}
			}
		};
		
		simpleRadio.addValueChangeHandler(simpleOrCQLHandler);
		cqlRadio.addValueChangeHandler(simpleOrCQLHandler);
		
		setLemmatized(false);
		
		setSimple();
	}
	
	public void setSimple() {
		this.isSimple = true;
		
		this.main.remove(cql);
		this.main.add(simple);
		this.main.add(radioPhrazeOrLemma);
	}
	
	public void setSimple(String query) {
		setSimple();
		this.simple.setValue(query);
	}
	
	public void setCQL() {
		this.isSimple = false;
		this.main.add(cql);
		this.main.remove(simple);
		this.main.remove(radioPhrazeOrLemma);
	}
	
	public void setCQL(String query) {
		setCQL();
		this.cql.setValue(query);
	}
	
	public void setLemmatized(boolean lemmatized) {
		this.isLemmatized = lemmatized;
		if (!lemmatized) {
			this.phraze.setValue(true);
			this.lemma.setEnabled(false);
		} else {
			this.lemma.setEnabled(true);
		}
	}
	
	public boolean isSimple() {
		return this.isSimple;
	}
	
	public boolean validate() {
		clearError();
		
		if (this.isSimple) {
			if (this.simple.getValue().length() <= 0) {
				this.simple.addStyleName(Resources.INSTANCE.css().formError());
				return false;
			}
		} else {
			if (this.cql.getValue().length() <= 0) {
				this.cql.addStyleName(Resources.INSTANCE.css().formError());
				return false;
			}
		}
		return true;
	}
	
	public void clearError() {
		this.simple.removeStyleName(Resources.INSTANCE.css().formError());
		this.cql.removeStyleName(Resources.INSTANCE.css().formError());
	}

	public void setAttributes(AttributItem[] attributs) {
		setLemmatized(false);
		for(AttributItem attribute : attributs){
			if(attribute.getAttributName().equals("lemma")) setLemmatized(true);
		}
	}

	public boolean isLemmatized() {
		return this.isLemmatized;
	}
	
	public String getCQL() {
		return this.cql.getValue();
	}
	
	public String getSimple() {
		return this.simple.getValue();
	}
	
	public boolean isLemma() {
		return this.lemma.getValue();
	}

	protected void setIsLemmatized(boolean lemmatized) {
		//return this.isLemmatized;
	}
	
	public CollectedParameters getParameters() {
		if(isSimple) return new CollectedSimpleOrCqlParameters(getSimple(), isLemma()?"lemma":"word", isLemmatized());
		else return new CollectedSimpleOrCqlParameters(getCQL(), isLemmatized());
	}

	public void setParameters(CollectedParameters params) {
		setIsLemmatized(((CollectedSimpleOrCqlParameters)params).isLemmatized());
		if(((CollectedSimpleOrCqlParameters)params).isSimple()) setSimple(((CollectedSimpleOrCqlParameters)params).getPhrase());
		else setCQL(((CollectedSimpleOrCqlParameters)params).getCql());
	}
}
